package com.joeyzh.soundrecorder.bean;

import android.os.Environment;

import java.io.File;
import java.io.Serializable;

/**
 * Created by xhl on 2020/12/2
 *
 * @author xhl
 * @Description
 */

public class AudioModel implements Serializable {
    private static String FILE_TAG = "SoundRecorder";
    private static String RECORD_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + FILE_TAG;
    /**
     * 文件名
     */
    private String fileName;
    /**
     * 文件地址
     */
    private String filePath;
    /**
     * 时长
     */
    private long elpased = 0;
    /**
     * 文件
     */
    private File file;

    public AudioModel() {
    }

    public AudioModel(String filePath) {
        this.filePath = filePath;
    }

    public AudioModel(String fileName, long elpased) {
        this.fileName = fileName;
        this.elpased = elpased;
        setFileNameAndPath(fileName);
    }

    public static void setRecordPath(String recordPath) {
        RECORD_PATH = recordPath;
    }

    public static void setFileTag(String fileTag) {
        FILE_TAG = fileTag;
    }


    /**
     * 清空音频文件
     */
    public static void clearRecordingDirectory() {

        clearRecordingDirectory(new File(RECORD_PATH));
    }

    /**
     * 清空音频文件
     *
     * @param directory
     */
    public static void clearRecordingDirectory(File directory) {
        File[] files = directory.listFiles();
        if (files != null) {
            for (File file : files) {
                if (!file.isDirectory()) {
                    file.delete();
                } else {
                    clearRecordingDirectory(file);
                }
            }
        }
        directory.delete();
    }

    public void setFileNameAndPath(String fileName) {
        int count = 0;
        File f;

        File pathFile = new File(RECORD_PATH);
        if (!pathFile.exists()) {
            pathFile.mkdir();
        }
        do {
            count++;
            this.filePath = RECORD_PATH + File.separator + fileName;
            f = new File(filePath);
            this.file = f;
        } while (f.exists() && !f.isDirectory());
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public int getElpasedInt() {
        return (int) Math.round((double)elpased / 1000);
    }

    public long getElpased() {
        return elpased;
    }

    public void setElpased(long elpased) {
        this.elpased = elpased;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public static String getFileTag() {
        return FILE_TAG;
    }

    public static String getRecordPath() {
        return RECORD_PATH;
    }
}
